﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;

public partial class Web_FarmMaster : System.Web.UI.MasterPage
{
    protected string GetWeather()
    {
        string url = "http://www.weather.com.cn/html/weather/101121201.shtml";

        Regex regex;
        string content = "";
        Match mcTmp;
        //int k = 1;

        HttpWebResponse theResponse;
        WebRequest theRequest;


        theRequest = WebRequest.Create(url);
        try
        {
            theResponse = (HttpWebResponse)theRequest.GetResponse();

            using (System.IO.Stream sm = theResponse.GetResponseStream())
            {
                System.IO.StreamReader read = new System.IO.StreamReader(sm, Encoding.UTF8);
                content = read.ReadToEnd();
            }
        }
        catch (Exception)
        {
            content = "";
        }


        string parttenTmp = "<title>.+</title>";
        regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        mcTmp = regex.Match(content);

        if (mcTmp.Success)
        {
            string tmp = mcTmp.Groups[0].Value;
            tmp = tmp.Substring(tmp.LastIndexOf(":") + 1).Trim();

            string[] ts = tmp.Split(' ');

            return string.Format("{0}&nbsp;&nbsp;&nbsp;&nbsp;{1}", ts[2].Trim(), ts[4].Trim());
        }
        else
        {
            return "";
        }
    }

    protected string GetDay()
    {
        return string.Format("{0:yyyy-MM-dd}", DateTime.Now);
    }

    protected string GetDate()
    {
        string[] ds = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

        return ds[(int)DateTime.Now.DayOfWeek];
    }

    protected string GetChineseDay(){
        ChineseLunisolarCalendar c = new ChineseLunisolarCalendar();

        int month = c.GetMonth(DateTime.Now);
        int day = c.GetDayOfMonth(DateTime.Now);

        string[] ms = { "", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
        string[] ds = { "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", 
                          "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", 
                          "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"};

        return string.Format("{0}月{1}", ms[month - 1], ds[day - 1]);
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected string GetTopicId(string name)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.Name.Equals(name) select aT;
        if (t.Count() != 0)
        {
            return t.First().ID;
        }
        else
        {
            return "";
        }
    }
    protected string GetArticleId(string name)
    {
        CmsDataContext db = new CmsDataContext();
        string topic_id = GetTopicId(name);
        string article_id = (from aA in db.Article where aA.TopicID.Equals(topic_id) select aA.ID).First();
        return article_id;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (SearchBox2.Text != "" && SearchBox2.Text != null)
        {
            Session["content"] = SearchBox2.Text;
            Response.Redirect("search.aspx");
        }
        else
        {
            SearchBox2.Text = "请输入搜索关键字";
        }
    }
}
